import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:grint/constant/colors.dart';
import 'package:grint/widget/my_text.dart';

class AuthPromptDialog extends StatelessWidget {
  final String title;
  final String message;
  final String? customActionText;
  final VoidCallback? onCustomAction;

  const AuthPromptDialog({
    super.key,
    this.title = 'Authentication Required',
    this.message = 'Please log in to continue using this feature.',
    this.customActionText,
    this.onCustomAction,
  });

  @override
  Widget build(BuildContext context) {
    return Material(
      color: Colors.transparent,
      child: Center(
        child: Container(
          width: 320.w,
          margin: EdgeInsets.symmetric(horizontal: 24.w),
          decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.circular(20.r),
            boxShadow: [
              BoxShadow(
                color: Colors.black.withValues(alpha: 0.1),
                blurRadius: 20,
                offset: const Offset(0, 10),
              ),
            ],
          ),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              // 顶部装饰区域
              Container(
                width: double.infinity,
                height: 80.h,
                decoration: BoxDecoration(
                  gradient: const LinearGradient(
                    colors: [Color(0xFF98F2C2), Color(0xFFAEE7EB)],
                    begin: Alignment.centerLeft,
                    end: Alignment.centerRight,
                  ),
                  borderRadius: BorderRadius.only(
                    topLeft: Radius.circular(20.r),
                    topRight: Radius.circular(20.r),
                  ),
                ),
                child: Center(
                  child: Container(
                    width: 48.w,
                    height: 48.w,
                    decoration: BoxDecoration(
                      color: Colors.white.withValues(alpha: 0),
                      shape: BoxShape.circle,
                    ),
                    child: Image.asset(
                      'assets/images/logo-small.png',
                      width: 28.w,
                      height: 28.w,
                    ),
                  ),
                ),
              ),

              Padding(
                padding: EdgeInsets.all(24.w),
                child: Column(
                  children: [
                    MyText(
                      text: title,
                      type: TextType.rubik,
                      fontSize: 20.sp,
                      fontWeight: FontWeight.w700,
                      color: AppColors.black,
                      textAlign: TextAlign.center,
                    ),

                    SizedBox(height: 12.h),

                    MyText(
                      text: message,
                      type: TextType.miSans,
                      fontSize: 14.sp,
                      fontWeight: FontWeight.w400,
                      color: AppColors.textGrey,
                      textAlign: TextAlign.center,
                      maxLines: 3,
                    ),

                    SizedBox(height: 24.h),

                    Row(
                      children: [
                        Expanded(
                          child: GestureDetector(
                            onTap: () => Navigator.of(context).pop(),
                            child: Container(
                              height: 44.h,
                              decoration: BoxDecoration(
                                color: const Color(0xFFF2F2F2),
                                borderRadius: BorderRadius.circular(12.r),
                              ),
                              child: Center(
                                child: MyText(
                                  text: 'Cancel',
                                  type: TextType.miSans,
                                  fontSize: 16.sp,
                                  fontWeight: FontWeight.w600,
                                  color: AppColors.textGrey,
                                ),
                              ),
                            ),
                          ),
                        ),

                        SizedBox(width: 12.w),

                        Expanded(
                          child: GestureDetector(
                            onTap: () {
                              Navigator.of(context).pop();
                              if (onCustomAction != null) {
                                onCustomAction!();
                              }
                            },
                            child: Container(
                              height: 44.h,
                              decoration: BoxDecoration(
                                gradient: const LinearGradient(
                                  colors: [
                                    Color(0xFF98F2C2),
                                    Color(0xFFAEE7EB),
                                  ],
                                  begin: Alignment.centerLeft,
                                  end: Alignment.centerRight,
                                ),
                                borderRadius: BorderRadius.circular(12.r),
                              ),
                              child: Center(
                                child: MyText(
                                  text: customActionText ?? 'Log In',
                                  type: TextType.miSans,
                                  fontSize: 16.sp,
                                  fontWeight: FontWeight.w600,
                                  color: Colors.white,
                                ),
                              ),
                            ),
                          ),
                        ),
                      ],
                    ),
                  ],
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

class AuthPromptHelper {
  static Future<void> showAuthPrompt({
    required BuildContext context,
    String? title,
    String? message,
    String? actionText,
    VoidCallback? onAction,
  }) {
    return showDialog(
      context: context,
      barrierDismissible: true,
      builder:
          (BuildContext dialogContext) => AuthPromptDialog(
            title: title ?? 'Authentication Required',
            message: message ?? 'Please log in to continue using this feature.',
            customActionText: actionText,
            onCustomAction: onAction,
          ),
    );
  }

  static Future<void> showPublishAuthPrompt({
    required BuildContext context,
    VoidCallback? onGoToAuth,
  }) {
    return showAuthPrompt(
      context: context,
      title: 'Login Required',
      message:
          'You need to log in to publish content. Please log in to continue.',
      actionText: 'Go to Login',
      onAction: onGoToAuth,
    );
  }

  static Future<void> showLikeAuthPrompt({
    required BuildContext context,
    VoidCallback? onGoToAuth,
  }) {
    return showAuthPrompt(
      context: context,
      title: 'Login Required',
      message: 'You need to log in to like content. Please log in to continue.',
      actionText: 'Go to Login',
      onAction: onGoToAuth,
    );
  }

  static Future<void> showCommentAuthPrompt({
    required BuildContext context,
    VoidCallback? onGoToAuth,
  }) {
    return showAuthPrompt(
      context: context,
      title: 'Login Required',
      message: 'You need to log in to comment. Please log in to continue.',
      actionText: 'Go to Login',
      onAction: onGoToAuth,
    );
  }

  static Future<void> showProfileAuthPrompt({
    required BuildContext context,
    VoidCallback? onGoToAuth,
  }) {
    return showAuthPrompt(
      context: context,
      title: 'Login Required',
      message:
          'You need to log in to view your profile. Please log in to continue.',
      actionText: 'Go to Login',
      onAction: onGoToAuth,
    );
  }

  static Future<void> showChallengeAuthPrompt({
    required BuildContext context,
    VoidCallback? onGoToAuth,
  }) {
    return showAuthPrompt(
      context: context,
      title: 'Login Required',
      message:
          'You need to log in to participate in challenges. Please log in to continue.',
      actionText: 'Go to Login',
      onAction: onGoToAuth,
    );
  }

  static Future<void> showFeatureAuthPrompt(
    String featureName, {
    required BuildContext context,
    VoidCallback? onGoToAuth,
  }) {
    return showAuthPrompt(
      context: context,
      title: 'Login Required',
      message:
          'You need to log in to use $featureName. Please log in to continue.',
      actionText: 'Go to Login',
      onAction: onGoToAuth,
    );
  }
}
